import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:shopping_group/base/base_constant.dart';
import 'package:shopping_group/entity/count_entity.dart';
import 'package:shopping_group/entity/widget_entity.dart';
import 'package:shopping_group/modle/login/bloc/user_certification_bloc.dart';
import 'package:shopping_group/modle/login/bloc/user_modify_password_bloc.dart';
import 'package:shopping_group/res/colors.dart';
import 'package:shopping_group/res/dimens.dart';
import 'package:shopping_group/utils/object_util.dart';
import 'package:shopping_group/utils/widget_utils.dart';
import 'package:shopping_group/view/button_progress_view.dart';
import 'package:bot_toast/bot_toast.dart';

class UserModifyPassWordPage extends StatefulWidget {
  @override
  _UserModifyPassWordPageState createState() => _UserModifyPassWordPageState();
}

class _UserModifyPassWordPageState extends State<UserModifyPassWordPage> {
  // WidgetEntity _oldPasswordWidgetEntity = WidgetEntity();
  TextEditingController _controllerCode = TextEditingController();
  WidgetEntity _passWordWidgetEntity = WidgetEntity();
  WidgetEntity _nextPassWordWidgetEntity = WidgetEntity();
  UserModifyPasswordBloc _bloc;
  CountEntity _countEntity = CountEntity();
  UserCertificationBloc _certificationBloc;
  @override
  void initState() {
    super.initState();
    // _oldPasswordWidgetEntity = WidgetUtils.initEditText(
    //     '原密码：', '', '请输入原密码', 'all', TextEditingController());
    _passWordWidgetEntity = WidgetUtils.initEditText(
        '新密码：', '', '请输入新密码', 'all', TextEditingController());
    _nextPassWordWidgetEntity = WidgetUtils.initEditText(
        '确认密码：', '', '请再次输入新密码', 'all', TextEditingController());
    _certificationBloc = UserCertificationBloc();
    _bloc = UserModifyPasswordBloc();
  }

  @override
  Widget build(BuildContext context) {
    // _context = context;
    return Scaffold(
        appBar: WidgetUtils.showAppar(context, '修改密码'),
        body: GestureDetector(
            behavior: HitTestBehavior.translucent,
            onTap: () {
              // 触摸收起键盘
              FocusScope.of(context).requestFocus(FocusNode());
            },
            child: Container(
              decoration: new BoxDecoration(
                color: Colors.white,
              ),
              child: SingleChildScrollView(
                  scrollDirection: Axis.vertical,
                  reverse: false,
                  padding: EdgeInsets.all(0.0),
                  physics: BouncingScrollPhysics(),
                  child: Center(
                    child: _registerBody(),
                  )),
            )));
  }

  Widget _registerBody() {
    return DefaultTextStyle(
      style: TextStyle(color: Colors.black),
      child: Container(
        child: Column(
          children: <Widget>[
            SizedBox(
              height: 32,
            ),
            WidgetUtils.itmeStyleView(
              '手机号：',
              BaseConstant.code,
              bewteenleft: 16,
            ),
            Row(
              mainAxisAlignment: MainAxisAlignment.center,
              crossAxisAlignment: CrossAxisAlignment.center,
              children: <Widget>[
                Expanded(
                  flex: 1,
                  child: Container(
                    padding: const EdgeInsets.only(left: 16),
                    alignment: FractionalOffset.centerLeft,
                    child: WidgetUtils.getTextColorView(
                        '验证码：', Colours.text_2222, Dimens.FONT_SIZE_16),
                  ),
                ),
                Expanded(
                  flex: 2,
                  child: StreamBuilder(
                    stream: _certificationBloc.pushDate,
                    initialData: _countEntity,
                    builder:
                        (BuildContext context, AsyncSnapshot<Object> snapshot) {
                      Object object = snapshot.data;
                      if (object is CountEntity) {
                        _countEntity = object;
                      }
                      return Padding(
                        padding: const EdgeInsets.only(left: 16),
                        child: WidgetUtils.buildTextFieldCode(
                            _controllerCode,
                            TextInputType.phone,
                            '请输入验证码',
                            List<TextInputFormatter>()
                              ..add(LengthLimitingTextInputFormatter(18)),
                            _countEntity.codeCountdownStr,
                            _countEntity.colorStr, codeCallback: (callback) {
                          _usercode();
                        }),
                      );
                    },
                  ),
                ),
              ],
            ),
            WidgetUtils.drawCodeDivider(height: 1),
            // WidgetUtils.showEditText(_oldPasswordWidgetEntity, isSecrecy: true),
            WidgetUtils.showEditText(_passWordWidgetEntity, isSecrecy: true),
            WidgetUtils.showEditText(_nextPassWordWidgetEntity,
                isSecrecy: true),
            const Padding(
              padding: EdgeInsets.only(top: 30.0),
            ),
            ButtonProgressView.crateCupertinoView('提交', cupertinoCall: () {
              FocusScope.of(context).requestFocus(FocusNode());
              _usercommit();
            }),
          ],
        ),
      ),
    );
  }

//校验获取验证码格式
  void _usercode() {
    _certificationBloc.verificationDate.add(BaseConstant.code);
  }

// 校验提交格式
  void _usercommit() {
    String varcode = _controllerCode.text;
    // String oldPwd = _oldPasswordWidgetEntity.controller.text;
    String pwdname = _passWordWidgetEntity.controller.text;
    String pwdNextname = _nextPassWordWidgetEntity.controller.text;
    if (ObjectUtil.isEmptyString(varcode)) {
      BotToast.showText(text: '请输入验证码');
      return;
    }

    if (ObjectUtil.isEmptyString(pwdname)) {
      BotToast.showText(text: '请输入密码');
      return;
    }

    if (ObjectUtil.isEmptyString(pwdNextname)) {
      BotToast.showText(text: '请再次输入密码');
      return;
    }

    if (pwdname.length < 6) {
      BotToast.showText(text: '密码至少6位');
      return;
    }

    if (pwdNextname.length < 6) {
      BotToast.showText(text: '确认密码至少6位');
      return;
    }
    if (pwdNextname != pwdname) {
      BotToast.showText(text: '两次密码不一致');
      return;
    }
    Map<String, dynamic> data = Map();
    data['userId'] = BaseConstant.userId;
    data['phone'] = BaseConstant.phone;
    data['validateCode'] = varcode;
    data['newPwd'] = pwdname;
    data['reNewPwd'] = pwdNextname;
    _bloc.saveDate.add(data);
  }

  @override
  void dispose() {
    super.dispose();
    // _oldPasswordWidgetEntity.controller?.clear();
    _passWordWidgetEntity.controller?.clear();
    _nextPassWordWidgetEntity.controller?.clear();
  }
}
